<!--
 * @name: 
 * @version: 1.1.1
 * @Author: 刘大可
 * @Date: 2022-03-30 12:08:30
 * @LastEditors: 刘大可
 * @LastEditTime: 2023-12-08 18:52:21
-->
<template>

	<view class="pages" :style="{'background-image':'url('+bg+')'}">
		<cu-custom bgColor="bgmain" :isBack="true">
			<view slot="backText"></view>
			<view slot="content">登录</view>
		</cu-custom>

		<view class="loginBottom flex-col">

			<view class="xieyi row" v-if="1==2">

				<view class="cuIcon-square icon" v-if="showPrivacy">

					<button id="agree-btn" open-type="agreePrivacyAuthorization"
						@agreeprivacyauthorization="handleAgreePrivacyAuthorization">同意</button>


				</view>
				<view class="cuIcon-squarecheckfill icon" v-else></view>
				<view @click="handleOpenPrivacyContract">我已阅读并同意《小程序隐私保护协议》
					<!-- {{ xieyi }} -->
				</view>
			</view>
			<!-- <view class="login"  @tap="bindgetuserinfo">登录</view> -->
			<view class="login" @tap="showLogin">登录</view>

		</view>
		<dake-agreement ref="Auth" callBack="callBack"></dake-agreement>

		<!-- isAuth -->
		<school-login :isAuth="isAuth"></school-login>
	</view>
</template>
<script>
	var app = getApp();

	export default {
		data() {
			return {
				bg: "",
				userInfo: {},
				isAuth: false,
				showPrivacy: false,
				xieyi: ""
			}
		},
		async onLoad(options) {
			await this.$refs.Auth.checkAuth()
			uni.$on("callBacck", res => {
				console.log(123);
			})
			// wx.getPrivacySetting({
			// 	success: res => {
			// 		console.log(res) // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
			// 		if (res.needAuthorization) {
			// 			return
			// 			// 需要弹出隐私协议
			// 			// this.showPrivacy= true
			// 		} else {
			// 			// resolve(res)

			// 			// 用户已经同意过隐私协议，所以不需要再弹出隐私协议，也能调用已声明过的隐私接口
			// 			// wx.getUserProfile()
			// 			// wx.chooseMedia()
			// 			// wx.getClipboardData()
			// 			// wx.startRecord()
			// 		}
			// 	},
			// 	fail: () => { },
			// 	complete: () => { }
			// })



			// // this.isAuth=true
			// wx.getPrivacySetting({
			// 	success: res => {
			// 		console.log(res) // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
			// 			this.xieyi=res.privacyContractName
			// 			this.showPrivacy = res.needAuthorization
			// 		if (res.needAuthorization) {
			// 			// 需要弹出隐私协议
			// 			// this.showPrivacy= true
			// 		} else {
			// 			// 用户已经同意过隐私协议，所以不需要再弹出隐私协议，也能调用已声明过的隐私接口
			// 			// wx.getUserProfile()
			// 			// wx.chooseMedia()
			// 			// wx.getClipboardData()
			// 			// wx.startRecord()
			// 		}
			// 	},
			// 	fail: () => { },
			// 	complete:() => {}
			// })

			// this.handleOpenPrivacyContract()

			this.getLocations()

			let sys = uni.getStorageSync('sys')
			this.bg = sys.xcx_login_bg
			this.loginCheck()
			this.isAuth = true
			uni.$on('loginCell', (data) => {
				console.log(data);
				if (data.userInfo) {
					this.bindgetuserinfo(data)
				}
				this.isAuth = false
			})
		},
		methods: {
			handleAgreePrivacyAuthorization(e) {
				console.log(e);
				this.showPrivacy = false
				// 用户同意隐私协议事件回调
				// 用户点击了同意，之后所有已声明过的隐私接口和组件都可以调用了
				wx.getUserProfile()
				// wx.chooseMedia()
				// wx.getClipboardData()
				// wx.startRecord()
			},
			handleOpenPrivacyContract() {
				// 打开隐私协议页面
				wx.openPrivacyContract({
					success: () => {}, // 打开成功
					fail: () => {}, // 打开失败
					complete: () => {}
				})
			},

			getCode() {
				return new Promise((success, fail) => {
					uni.login({
						provider: "weixin",
						success,
						fail
					})
				})

			},
			async loginCheck() {
				let {
					code
				} = await this.getCode()
				console.log(code);
				app.globalData.util.request({
					url: 'login/loginCheck',
					data: {
						code: code || ""
					},
					success: (res) => {
						console.log("配置信息", res.data)
						if (res.data.status == 200) {
							if (res.data.token) {
								console.log("配置信息", res.data.token)

								res.data.data = res.data.token
								// uni.setStorageSync('token', res.data.data.token)
								// this.islogin = true
								this.userInfo = res.data.data
								uni.setStorageSync('userrs', res.data.data)
							}
						}
						// console.log(res.data.data.sys.logo);


						// that.msg();
					}
				});
			},
			showLogin() {

				// if (this.showPrivacy) {
				// 	this.tips.tips("请先阅读并同意《小程序隐私保护协议》")
				// 	return false
				// }

				if (this.userInfo.u_id) {
					this.bindgetuserinfo({
						userInfo: {
							avatarUrl: this.userInfo.avatar,
							nickName: this.userInfo.nickname
						}
					})

				} else {
					this.isAuth = true
				}

			},
			//用户授权登录
			bindgetuserinfo(res = {}) {
				console.log('打印', res);
				var that = this;
				uni.getUserProfile({
					desc: '用于获取用户信息',
					success(res) {
						console.log(res)


					}
				});

				uni.login({
					success(code) {
						// res.userInfo={}
						console.log(code)
						app.globalData.util.request({
							url: 'Login/miniLogin',
							data: {
								code: code.code,
								nickName: res.userInfo.nickName || '微信用户',
								avatar: res.userInfo.avatarUrl,
								spid: uni.getStorageSync('spid'),
								s_id: uni.getStorageSync('schoolId')
							},
							success(user) {
								uni.setStorageSync('userinfo', res.userInfo)
								that.loginuserinfo = res.userInfo
								that.setData({
									islogin: true
								})
								console.log(user.data.token)
								uni.setStorageSync('token', user.data.token)
								that.loginCheck()

								// that.getUser()
								var duration = 1000
								uni.showToast({
									title: "登录成功",
									duration,
									mask: true,
									success: (res) => {
										setTimeout(() => {
											uni.navigateBack({
												delta: 1
											})
										}, duration);
									}
								});

							},
							fail(rej){
								this.isAuth = true
							}
							
						})
					}
				})
			},

		},
	}
</script>
<style lang="scss">
	page {
		// background-image: url('https://testa.fkynet.net/wximage/login_bg.jpg');

	}

	.pages {
		display: flex;
		flex-direction: column;
		height: 100vh;
		background-size: auto 100%;
		background-repeat: no-repeat;
		background-position: center;

	}


	.loginBottom {
		margin-top: auto;

		.xieyi {
			justify-content: center;
			margin-bottom: 20rpx;

			.icon {
				position: relative;
				margin-right: 10rpx;
				font-size: 40rpx;

				#agree-btn {
					position: absolute;
					top: 0px;
					left: 0px;
					width: 100%;
					height: 100%;
					opacity: 0;
				}
			}
		}

		.login {
			width: 600rpx;
			height: 100rpx;
			background-color: $main_color;
			border-radius: 10rpx;
			margin-bottom: 150rpx;
			margin: auto auto 200rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #fff;
			font-size: 40rpx;
		}
	}
</style>